package org.demo.tmp;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;



/** 
 * 线程同步测试
 * 
 * @author
 * @date 2010-12-5
 * @file org.demo.tmp.Test.java
 */
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String str = "//abc/def/hij";
		int firstSlash = str.indexOf('/');
		int lastSlash = str.indexOf('/', firstSlash + 1);
		System.out.println(firstSlash);
		System.out.println(lastSlash);
	}
	public static void testOracleMeta() throws Exception {
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@202.110.200.130:1521:orcl";
		String user = "premid";
		String pass = "premid";
		// 
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url, user, pass);
		DatabaseMetaData dbmd = conn.getMetaData();
		ResultSet rs = dbmd.getColumns(conn.getCatalog(), "PREMID", "TMP_TEST", null);
		while(rs.next()){
			String column_name = rs.getString("COLUMN_NAME");
			int data_type = rs.getInt("DATA_TYPE");
			String type_name = rs.getString("TYPE_NAME");
			System.out.println(column_name + " " + data_type + " " + type_name);
		}	
	}
	public static String encryptBase64(byte[] key){
        String res = new BASE64Encoder().encodeBuffer(key);
        int len = res.length();      
        res = res.substring(0,len-2);              // 删除末尾的 \r\n
        return res;
    }
	/**
     * base64 解码
     * @param key :String
     * @return byte[]
     */
    public static byte[] decryptBase64(String key){
        byte[] plaintext = null;
        try {
            plaintext = new BASE64Decoder().decodeBuffer(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return plaintext;
    }
	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection()throws Exception{
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@10.162.11.134:1521:xe";
		String user = "hr";
		String pass = "hr";
		// 
		Class.forName(driver);
		return DriverManager.getConnection(url, user, pass);
	}
	/**
	 * 判断一个数是否是素数
	 * @param number
	 * @return
	 */
	public static boolean isPrime(int number){
		
		if (number < 2){
			// <= 1
			return false;
		} else if (number < 4){
			// 2,3
			return true;
		}
		// other number >= 4
		boolean flag = true;
		int mid = number/2;
		int current = 2;
		while(current <= mid){
			if (number%current == 0){
				flag = false;
				break;
			}
			current++;
		}
		return flag;
	}
}
